import pygame
# 我方飞机
import ship
# 敌方飞机
from enemy import Enemy

from achievement import AchieveMent

from bg import Bg

from gamecontrol import GameControl

# 导入设置
from settings import *
# 导入颜色
from bg_color import *
# 导入事件类型
from eventType import *

pygame.init()

# 屏幕
screen = pygame.display.set_mode((780, 560))
screen_rect = screen.get_rect()

pygame.display.set_caption("My Game")

game_control = GameControl(screen, screen_rect)

running = True

paused = False

#背景
bg_frame = Bg(screen, screen_rect)

# 飞机
hero = ship.Hero(screen, screen_rect)

# 歼敌记录(成就)
avm = AchieveMent(screen, screen_rect)

# 敌机集合
enemys = pygame.sprite.Group()
enemys.add(Enemy('images/enemy1.png', 4, -90, 0.22,1,screen,avm))
pygame.time.set_timer(EVENT_ADD_ENEMY, 2000)


# 关联敌机
hero.connect(enemys)

def main_loop(running):
    # 全局函数在函数内部使用时，需要这一句来访问全局变量
    global paused

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    paused = not paused
            elif event.type == EVENT_ADD_ENEMY:
                em = Enemy('images/enemy1.png', 4, -90, 0.22, 1, screen,avm)
                enemys.add(em)
            elif event.type == EVENT_ADD_HUGE_ENEMY:
                em = Enemy('images/enemy2.png', 8, 180, 0.12, 2, screen,avm)
                enemys.add(em)
                
        screen.fill(bg_color1)


        if not paused:
            bg_frame.update()
            # 飞机移动
            hero.update()

            avm.display()

            # 更新敌机的位置
            for em in enemys:
                em.update()
        else:
            game_control.pause()

        # 刷新屏幕
        pygame.display.flip()

        # 帧率
        pygame.time.Clock().tick(REFRESH_FPS)




main_loop(running)



pygame.quit()
